﻿using System;
using System.Drawing;
using System.IO;
using System.Net;

namespace PocketGPW
{
    internal static class ImageHelper
    {
        /// <summary>
        /// Pobieranie zdjęcia z podanego Uri.
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        internal static Image FromUri(Uri uri)
        {
            Image image = null;
            HttpWebRequest request;
            HttpWebResponse response;

            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(uri);
            }
            catch
            {
                return null;
            }

            try
            {
                response = (HttpWebResponse)request.GetResponse();
            }
            catch
            {
                return null;
            }

            try
            {
                using (Stream s = response.GetResponseStream())
                {
                    image = new Bitmap(s);
                    s.Close();
                }
            }
            catch { }

            response.Close();
            request.Abort();
            response = null;
            request = null;

            return image;
        }
    }
}
